﻿@page "/js-prompt"

@namespace SageKing.UI.Razor.Base.JsCollocation
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using SageKing.Core.Contracts
@implements IAsyncDisposable

@inject IJSRuntime Js

<PageTitle>Js collocation 1</PageTitle>

<h1>js collocation example 1</h1>

<button @onclick="showprompt">Call show prompt1</button>

@if (!string.IsNullOrEmpty(result))
{
	<p>
		Hello @result!
	</p>
}

@if (windowSize != null)
{
	<ul>
		<li>availScreenHeight:@windowSize.availScreenHeight</li>
		<li>availScreenWidth:@windowSize.availScreenWidth</li>
		<li>screenHeight:@windowSize.screenHeight</li>
		<li>screenWidth:@windowSize.screenWidth</li>
		<li>windowInnerHeight:@windowSize.windowInnerHeight</li>
		<li>windowInnerWidth:@windowSize.windowInnerWidth</li>
		<li>windowInnerHeightNoScroll:@windowSize.windowInnerHeightNoScroll</li>
		<li>windowInnerWidthNoScroll:@windowSize.windowInnerWidthNoScroll</li>
		<li>windowOuterHeight:@windowSize.windowOuterHeight</li>
		<li>windowOuterWidth:@windowSize.windowOuterWidth</li>
		<li>pageHeight:@windowSize.pageHeight</li>
		<li>pageWidth:@windowSize.pageWidth</li>
	</ul>
}

@code {
	private IJSObjectReference? module;

	private string? result;
	private SKWindowSize? windowSize;

	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			try
			{
				string path = "./Pages/JsCollocation/JsComm.razor.js";
				module = await Js.InvokeAsync<IJSObjectReference>("import", path);

				if (module is not null)
				{
					windowSize = await module.InvokeAsync<SKWindowSize>("windowSize");
					StateHasChanged();
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}
	}
	async ValueTask IAsyncDisposable.DisposeAsync()
	{
		if (module is not null)
		{
			await module.DisposeAsync();
		}
	}

	public async void showprompt(MouseEventArgs e)
	{
		try
		{
			if (module is null)
			{
				return;
			}

			result = await module.InvokeAsync<string>(
				"showPrompt", "What' your name?");

			StateHasChanged();
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.Message);
		}
	}
}
